package org.kunlong.qgen.parser;

import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Element;
import org.kunlong.qgen.domain.NonChoiceQuestion;
import org.kunlong.qgen.domain.QuestionHolder;

public abstract class AbstractNoneChoiceQuestionParser extends AbstractParser {

	private static final int DEFAULT_BLANK_LINE = 5;

	@SuppressWarnings("unchecked")
	public void parse(Element e, QuestionHolder holder) throws Exception {
		NonChoiceQuestion nonChoiceQuestion = new NonChoiceQuestion();
		nonChoiceQuestion.setQuestionType(getQuestionType());
		nonChoiceQuestion.setAnswer(getAnswer(e));
		nonChoiceQuestion.setDescription(getDescription(e));
		Attribute a = e.attribute("blank_line");
		if (a == null) {
			nonChoiceQuestion.setBlank_line(DEFAULT_BLANK_LINE);// default value
		} else {
			try {
				String blank_lineStr = a.getText().trim();
				nonChoiceQuestion
						.setBlank_line(Integer.parseInt(blank_lineStr));
			} catch (Exception ex) {
				//any problem set the blank_line to default
				nonChoiceQuestion.setBlank_line(DEFAULT_BLANK_LINE);
			}
		}
		//get all Notice element
		List<Element> noticeElements = e.elements("Notice");
		for (Element noticeElement : noticeElements) {
			//add all notices
			String note = noticeElement.getText();
			if (null != note && !"".equals(note))
				nonChoiceQuestion.getNoticeList().add(note);
		}
		//add question to holder
		holder.add(getQuestionType(), nonChoiceQuestion);
	}

}
